<?php
/**
 * function.php - MySQL queries, formating massage and time output.
 *
 * NMIT BIT Project - Infinity - A 3D Website using WebGL & HTML5 Technology
 * Infinity Grid Version 
 * Version: 0.1.2
 *
 * Author: William Liu <qzliuyuzou@gmail.com> <lyzdev@gmail.com>
 *
 * Required Server Version:
 *  PHP5 or higher
 *  MySQL 5 or higher
 *
 * Required Browser Version:
 *  Google Chrome 9 or higher
 *  Mozilla Firefox 4 Beta or higher
 *  *Apple Safari
 *
 * Copyright (c) 2011, William Liu All rights reserved.
 * Infinity is an open source 3D website project
 * Under Apache License 2.0
 * http://www.lyzsoft.cn
 * http://7cloud.wordpress.com
 *
 * Project Starting date: 9/3/2011
 * Update Date 1: 19/3/2011
 * Update Date 2: 24/3/2011
 * Update Date 3: 6/5/2011
 * Update Date 4: 5/6/2011 - Beta 2
 *
**/
?>

<?php
//deny the access directly
if(!defined('INCLUDE_CHECK')) die('Access Forbidden! You are not allow to execute this file directly!');

function formatSay($msg,$dt,$avatarID)
{
	$say = str_replace("\n","<br />",htmlspecialchars(stripslashes($msg)));

	return'
		<tr>
			<td class="author">
				<strong>
	      			<img src="images/'.$avatarID.'.png" width="50" height="50" alt="demo" />
					<a href="#">People_'.$avatarID.'</a>
				</strong>
			</td>
			<td class="msg">
				<p style="word-break: break-all;">'. preg_replace('/((?:http|https|ftp):\/\/(?:[A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?[^\s\"\']+)/i','<a href="$1" rel="nofollow" target="blank">$1</a>',$say).'</p>
				<div class="date">'.tranTime($dt).'</div>	
			</td>
		</tr>
	';
	;
}

//time coversion
function tranTime($time)
{
	$rtime = date("m-d H:i",$time);
	$htime = date("H:i",$time);
	$time = time() - $time;

	if ($time < 60) {
		$str = 'Just Now';
	}
	elseif ($time < 60 * 60) {
		$min = floor($time/60);
		$str = $min.' Min Ago';
	}
	elseif ($time < 60 * 60 * 24) {
		$h = floor($time/(60*60));
		$str = $h.' Hour(s) Ago '.$htime;
	}
	elseif ($time < 60 * 60 * 24 * 3) {
		$d = floor($time/(60*60*24));
		if($d=1)
		   $str = 'Yesterday '.$rtime;
		else
		   $str = '2 Days Ago'.$rtime;
	}
    else {
		$str = $rtime;
	}
	return $str;
}
?>

<?php
//administration msg list, include delete button
function adminMsgList($msg,$dt,$avatarID,$msgID)
{
	$say = str_replace("\n","<br />",htmlspecialchars(stripslashes($msg)));
	
	return '
		<tr>
			<td class="author">
				<strong>
	      			<img src="images/'.$avatarID.'.png" width="50" height="50" alt="demo" />
					<a href="#">People_'.$avatarID.'</a>
				</strong>
			</td>
			<td class="msgadmin">
				<p>'. preg_replace('/((?:http|https|ftp):\/\/(?:[A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?[^\s\"\']+)/i','<a href="$1" rel="nofollow" target="blank">$1</a>',$say).'</p>
				<div class="date">'.tranTime($dt).'</div>	
			</td>
			<td class="delbtn">
				<a class="delbtn" href="admin.php?action=del&id='.$msgID.'">Delete</a>
			</td>
		</tr>
	';
}
?>


<?php
//file access checking function
//deny access directly
function checkAccess()
{
	$hostUrl = $_SERVER['SERVER_NAME'];
	if(isset($_SERVER['HTTP_REFERER']))
	{
		$mainUrlFilter = explode('://',$_SERVER['HTTP_REFERER']);
		$secondUrlFilter = explode('/',$url1[1]);
		if($hostUrl != $secondUrlFilter[0])
		{
			echo 'Access Forbidden! You are not allow to execute this file directly!';
			echo "<script language=\"JavaScript\">window.setTimeout(\"window.location.href=\'index.php\'\", 3000); </script>";
			exit();
			}
	}
else
{
	echo 'Access Forbidden! You are not allow to execute this file directly!';
	echo "<script language=\"JavaScript\">window.setTimeout(\"window.location.href=\'index.php\'\", 3000); </script>";
	exit();
	}
}
?>
